package com.mars.utils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTime {
    /**
     * 获取时间戳
     * @return
     */
    public static int getTime(){
        return  (int) (System.currentTimeMillis() / 1000);
    }

    /**
     * 时间转时间戳
     * @param date
     * @return
     */
    public static int getTime(String date){

        long time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(date, new ParsePosition(0)).getTime() / 1000;
        return  (int) time;
    }

    /**
     * 时间戳转时间
     * @param timeStamp
     * @return
     */
    public static String format(int timeStamp){
        long time = timeStamp;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(time*1000));      // 时间戳转换成时间
        return  sd;
    }

    public static void main(String[] args) {
        System.out.println(getTime("2019-06-15 09:32:49"));
        System.out.println(format(1560562369));
    }

}
